package com.eigaland.change_machine.business.setup_account

import com.eigaland.change_machine.APP
import com.eigaland.change_machine.BuildConfig
import com.eigaland.change_machine.business.network.RetrofitClient
import com.eigaland.change_machine.displaymodel.HomeDisplayModel
import com.eigaland.change_machine.displaymodel.IDisplayModel
import com.eigaland.change_machine.util.UserManager
import com.eigland.eigaland_lib.base.BaseRepository
import com.eigland.eigaland_lib.model.LoginDeviceRequest
import com.eigland.eigaland_lib.network.LoginService
import com.google.gson.Gson
import movie6.posprintercept2.extensions.saveCinemaId
import movie6.posprintercept2.extensions.saveToken
import java.util.logging.Logger

public class SetUpAccountFragmentRepository : BaseRepository() {

    suspend fun loginDevice(userName: String,userPwd:String,sec:String): HomeDisplayModel?{
        var request = LoginDeviceRequest(userName,userPwd,sec)

        com.orhanobut.logger.Logger.e("login json-->"+ Gson().toJson(request))
        var displayModel = HomeDisplayModel()
        var result = RetrofitClient.getService(LoginService::class.java, BuildConfig.domain).login(request).await()
        result.success?.let {
            displayModel.deviceSecret = it.device?.secret.toString()
            displayModel.loginStatus = IDisplayModel.SUCCESS_STATUS
            APP.app.saveToken(it.token) // save token use to add header in retrofit
            APP.app.saveCinemaId(it.cinema.uuid)
            UserManager.cinemaId = it.cinema.uuid
            UserManager.setCurUser(it)
        }
        result.error?.message?.let {
            displayModel.errorMsg =it
            displayModel.loginStatus = IDisplayModel.ERROR_STATUS
        }

        return displayModel

    }

    suspend fun loginAdmin(){

    }
}
